import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/user/login.vue'
import home from '@/views/home/home.vue'
import movie from '@/views/movie/movie.vue'
import qa from '@/views/qa/qa.vue'
import user from '@/views/user/user.vue'
import edit from '@/views/user/edit.vue'
import layout from '@/views/layout.vue'
import notfound from '../views/notfound/notfound.vue'
Vue.use(VueRouter)

const routes = [
  {
    name: 'login',
    path: '/login',
    component: login
  },
  {
    name: 'edit',
    path: '/edit',
    component: edit
  },
  {
    name:"layout",
    path: '/',
    component: layout,
    children:[
      {
        path: '/home',
        name: 'home',
        component: home
      },
      {
        path: '/movie',
        name: 'movie',
        component: movie
      },
      {
        path: '/qa',
        name: 'qa',
        component: qa
      },
      {
        path: '/user',
        name: 'user',
        component: user,
        // 路由元信息
        meta:{isLogin:true}
      },
    ]
  },
  {
    path:'/',
    redirect:'/home'
  }
  ,{
    name: 'notfound',
    path: '/notfound',
    component: notfound
  },{
    path:'*',
    redirect:'/notfound'
  }
]
const originPush =VueRouter.prototype.push
VueRouter.prototype.push=function(location,resolve,reject){
if(resolve||reject){
  return originPush(this,location,resolve,reject)
}else{
  return originPush.call(this,location).catch(err=>{
    // throw err
  })
}
}

const router = new VueRouter({
  routes
})
import {getToken} from '../utils/token'
router.beforeEach((to,from,next)=>{
  // user需要登录检查
  if(to.meta.isLogin&& !getToken()){
      next({path:"/login"})
  }else{
    next()
  }
})


export default router
